#include <STC15F2K60S2.H>
#include "Seg.h"
#include "Tim.h"
#include "iic.h"
#include "LED.h"
#include "Key.h"
#include "stdio.h"
//对at24c02和数模的运用，st24c02读数值的时候在初始化的时候读就好，写读都是char类型
//EEPROM_Read(pucInit, 1,4);这个就读一次赋值给数组就好，不用一直赋值
unsigned char j;
unsigned char ucNum;//实现不同等级可以定义一个变量自增对目标值比较，小于就置1，大于就置0
unsigned char ucpcf_U;
unsigned char ucLedi,ucLedj,ucLedk;
unsigned char pucSeg_Buf[9],pucSeg_Code[8],ucSeg_Pos;
unsigned char ucKey_Dly,ucKey_Old;
unsigned int ucSeg_Dly;
unsigned long ulms,uSms,ucStart;//注意好赋值的变量对应的类型
unsigned char ucLed;
unsigned char ucCort,ucState,ucSeg_Flag,ucSeg_C;
unsigned char pucInit[4];//at24c02要用char类型来实现读写
unsigned int pucBuf[4]={400,400,400,400};
unsigned char pucLed[4][8]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,
0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,
0x81,0x42,0x24,0x18,0x81,0x42,0x24,0x18,
0x18,0x24,0x42,0x81,0x18,0x24,0x42,0x81};//灯模式的转换数组
void Led_Proc(void);
void Key_Proc(void) ;
void Seg_Proc(void);
void Init_Proc(void);
void Time_Set(unsigned char ucKey_Down);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	 Init_Proc();
	while(1){
		Key_Proc();
		Led_Proc();
		Seg_Proc();
}
}
void Init_Proc(void){
	unsigned char i;
	EEPROM_Read(pucInit, 1,4);//这个不能循环一直写读
	for(i=0;i<4;i++){
		if(pucInit[i]>=4&&pucInit[i]<=12)
		pucBuf[i]=pucInit[i]*100;
	}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	 if(!(ulms % 1000)) //它没有清零
	 { 
		 
	} 
	 	if(ucpcf_U>ucNum)
			Led_Disp(ucLed) ;
		else
			Led_Disp(0) ;
	 if(++ucNum==5) ucNum=0;
	 if(++uSms==800) {
		 uSms=0;
		 ucSeg_Flag^=1;
	 }
	 if(++ucSeg_Dly==500) ucSeg_Dly=0;
	 if(++ucKey_Dly==10) ucKey_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 switch(ucKey_Down){
		 case 5:
			 ucCort^=1;
		 ucStart=ulms;
			break;
		 case 4:
			 if(++ucState==3) ucState=0;
			if(ucState==1) ucLedk=1;
			if(ucState==0) {
					for(j=0;j<4;j++)
						pucInit[j]=pucBuf[j]/100;
					EEPROM_Write(pucInit, 1, 4);
			}
			 break;
		  case 8:
			 Time_Set(ucKey_Down);
			break;
		 case 9:
			 Time_Set(ucKey_Down);
			break;
	 }
	 if( ucKey_Up==8){
		 if(ucState==0)
			 ucSeg_C^=1;
	 }
}
void Time_Set(unsigned char ucKey_Down){
	switch (ucState){
		case 0:
			if(ucKey_Down==8)
				ucSeg_C^=1;
			break;
		case 1:
			if(ucKey_Down==9){
				if(++ucLedk==5) ucLedk=1;//在分支语句中使用分支语句记得加括号，不然对应有影响
			}
				
			else if(ucKey_Down==8){
				if(--ucLedk==0) ucLedk=4;
			}
			break;
			case 2:
			if(ucKey_Down==9){
				if(pucBuf[ucLedk-1]<1200)
				pucBuf[ucLedk-1]+=100;
			}
			else if(ucKey_Down==8){
				if(pucBuf[ucLedk-1]>400)
				pucBuf[ucLedk-1]-=100;
			}
			break;
	}
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	if(ucSeg_C){
		sprintf(pucSeg_Buf,"      -%d",(unsigned int)(ucLedi+1));
	}
	else{
		switch(ucState){
		case 0:
			sprintf(pucSeg_Buf,"        ");
			break;
		case 1:
			if(ucSeg_Flag)//数码管运用改变的数组的值，下标变量用不是改变的
				sprintf(pucSeg_Buf,"-%1d- %4d",(unsigned int)ucLedk,(unsigned int)pucBuf[ucLedk-1]);
			else
			sprintf(pucSeg_Buf,"- - %4d",(unsigned int)pucBuf[ucLedk-1]);
			break;
			case 2:
			if(ucSeg_Flag)
				sprintf(pucSeg_Buf,"-%1d- %4d",(unsigned int)ucLedk,(unsigned int)pucBuf[ucLedk-1]);
			else
			sprintf(pucSeg_Buf,"-%1d-     ",(unsigned int)ucLedk);
			break;
	}
	}
	
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void Led_Proc(void){
	if(ucCort) {
		ucpcf_U=PCF8591_Adc()/51 ;
		if(ulms-ucStart>=pucBuf[ucLedi]){
		ucStart=ulms;//运用和ulms比较实现计时
		ucLed=pucLed[ucLedi][ucLedj];
		if(++ucLedj==8){
			ucLedj=0;
			if(++ucLedi==4)
				ucLedi=0;
		}
	}
	}
		else{
			ucLed=0;
			ucLedi=0;
			ucLedj=0;
		}
}